package main

import (
	"fmt"
	"log"

	"github.com/bytedance/sonic"
)

type User struct {
	ID    int      `json:"id"`
	Name  string   `json:"name"`
	Tags  []string `json:"tags,omitempty"`
	Email string   `json:"email"`
}

func main() {
	// 1. 序列化
	u := User{
		ID:    42,
		Name:  "Alice",
		Tags:  []string{"golang", "performance"},
		Email: "alice@example.com",
	}
	data, err := sonic.Marshal(u)
	if err != nil {
		log.Fatalf("marshal failed: %v", err)
	}
	fmt.Printf("JSON: %s\n", data)

	// 2. 反序列化
	var copy User
	if err := sonic.Unmarshal(data, &copy); err != nil {
		log.Fatalf("unmarshal failed: %v", err)
	}
	fmt.Printf("User struct: %+v\n", copy)

	// 3. 直接解析为 map
	var generic map[string]any
	if err := sonic.Unmarshal(data, &generic); err != nil {
		log.Fatalf("unmarshal to map failed: %v", err)
	}
	fmt.Printf("Generic: %#v\n", generic)
}
